package com.zj.core.dialog

import android.app.Activity
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import com.zj.core.R
import com.zj.core.adapter.TemplateListAdapter
import com.zj.core.base.BaseDialog
import com.zj.core.bean.VoiceTemplateBean
import com.zj.core.interfaces.TemplateTitleIml
import com.zj.core.tools.ViewPosition
import com.zj.core.ui.DialogTemplateUi

/**
 * Created by Administrator on 2019/5/6.
 */

@Suppress("NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class TemplateDialog (context: Activity?,type:Int) : BaseDialog(context),View.OnClickListener{
    private var mRowsBean: ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>? =ArrayList()
    private var mTemplate: TemplateListAdapter? = null
    private var callBackIml: TemplateTitleIml? = null
    private var mLayoutManager:LinearLayoutManager?=null
    private var isAllSelect = false
    private var mTitleStr:String=""
    private var mDialogType:Int=type
    private var templateDialog: DialogTemplateUi?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val width = ViewPosition().getScreenWidth(context)
        val height = (ViewPosition().getScreenHeight(context) * 0.95).toInt()
        val params = LinearLayout.LayoutParams(width, height, 0f)

        templateDialog= DialogTemplateUi(context, this)
        templateDialog?.apply {
            setContentView(view_root,params)

            //setCancelable(cancelable);
            setCanceledOnTouchOutside(true)

            mLayoutManager = LinearLayoutManager(context)
            mLayoutManager?.orientation = LinearLayoutManager.VERTICAL

            mTemplate = TemplateListAdapter(context, mRowsBean, mDialogType)
            template_list_rv?.layoutManager = mLayoutManager
            template_list_rv?.adapter = mTemplate

            if (mTitleStr != "") {
                common_dialog_title_tv?.text = mTitleStr
            }
        }
    }

    fun setItemBean(rowsBean: ArrayList<VoiceTemplateBean.VoiceWordBean.DatEntity.RowsEntity>) {
        this.mRowsBean = rowsBean
    }

    fun setDialogTitle(title: String){
        mTitleStr=title
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.common_dialog_all_check->{                    //全选
                if (!isAllSelect) {
                    templateDialog?.common_dialog_all_check!!.text = "反选"
                    isAllSelect = true
                } else {
                    templateDialog?.common_dialog_all_check!!.text = "全选"
                    isAllSelect = false
                }
                mTemplate?.setAllCheck(isAllSelect)
                mTemplate?.notifyDataSetChanged()
            }

            R.id.common_dialog_cancel_tv->dismiss()             //取消
            R.id.common_dialog_confirm_tv->valueForDialog()     //提交
        }
    }

    /**
     * dialog数据
     */
    private fun valueForDialog(){
        var mTitleArr = ""
        for (i in 0 until templateDialog?.template_list_rv!!.childCount) {
            val onConstraint = templateDialog?.template_list_rv!!.getChildAt(i) as ConstraintLayout
            if ((onConstraint.getChildAt(1) as CheckBox).isChecked) {
                if(mDialogType==0||mDialogType==2){                      //正常模板
                    mTitleArr = mTitleArr +(onConstraint.getChildAt(3) as TextView).text.toString() + "\n"
                }else if(mDialogType==1){                //项目模板
                    mTitleArr = mTitleArr +(onConstraint.getChildAt(2) as TextView).text.toString() + "、"
                }
            }
        }
        callBackIml?.callBackTitle(mTitleArr, mRowsBean?.get(0)?.meaning.toString())
        dismiss()
    }

    fun setCallBackIml(callBackIml: TemplateTitleIml) {
        this.callBackIml = callBackIml
    }

    override fun dismiss() {
        super.dismiss()
        if(mRowsBean!=null){
            mRowsBean?.clear()
            mRowsBean=null
        }

        if(mTemplate!=null){
            mTemplate=null
        }

        if(mLayoutManager!=null){
            mLayoutManager=null
        }
    }
}